package com.wanpinghui.supplier.business;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.business.view.PinchImageView;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayout;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.RxEventType;
import com.wanpinghui.wphlibrary.common.toast.ToastManager;
import com.wanpinghui.wphlibrary.model.business.BusinessService;
import com.wanpinghui.wphlibrary.model.business.bean.CommentBean;
import com.wanpinghui.wphlibrary.model.business.bean.DynamicBean;
import com.wanpinghui.wphlibrary.model.business.bean.ThumbUpBean;
import com.yunshl.yunshllibrary.rxbus.RxBus;
import com.yunshl.yunshllibrary.rxbus.SubscriptionBean;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlPopupWinow;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.LinkedList;
import java.util.List;

import retrofit.callback.YRequestCallback;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2018/1/29
 ***/
@ContentView(R.layout.layout_activity_dynamic_detail_video)
public class DynamicDetailVideoActivity extends BaseActivity {
    private static final int REQUEST_COMMENT = 0x15;
    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;
    @ViewInject(R.id.video_view)
    private VideoView videoView;
    @ViewInject(R.id.tv_content)
    private TextView textViewContent;
    @ViewInject(R.id.ll_comment)
    private LinearLayout mLayoutComment;
    @ViewInject(R.id.ll_detail)
    private LinearLayout mLayoutDetail;
    @ViewInject(R.id.ll_like)
    private LinearLayout mLayoutLike;
    @ViewInject(R.id.ll_bottom)
    private LinearLayout mLayoutBottom;

    final LinkedList<PinchImageView> viewCache = new LinkedList<PinchImageView>();

    private DynamicBean mDynamicBean;
    private boolean isPause;
    private View commentView;
    private YunShlPopupWinow popupWindow;
    private boolean isHide;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            String dataStr = getIntent().getStringExtra("data");
            if (TextUtil.isNotEmpty(dataStr)) {
                mDynamicBean = new Gson().fromJson(dataStr, new TypeToken<DynamicBean>(){}.getType());
            }
        }
    }

    @Override
    public void bindEvents() {
        mLayoutDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DynamicDetailVideoActivity.this, DynamicDetailActivity.class);
                intent.putExtra("data", new Gson().toJson(mDynamicBean));
                startActivity(intent);
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.start();
                mp.setLooping(true);
            }
        });

        videoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isHide) {
                    isHide = true;
                    ObjectAnimator.ofFloat(mLayoutTitle,"translationY",0,-DensityUtil.dip2px(46)).setDuration(400).start();
                    ObjectAnimator.ofFloat(mLayoutBottom,"translationY",0,DensityUtil.dip2px(36)).setDuration(400).start();
                }else {
                    isHide = false;
//                            mLayoutTitle.setVisibility(View.VISIBLE);
//                            mLayoutBottom.setVisibility(View.VISIBLE);
                    ObjectAnimator.ofFloat(mLayoutTitle,"translationY",-DensityUtil.dip2px(46),0).setDuration(400).start();
                    ObjectAnimator.ofFloat(mLayoutBottom,"translationY",DensityUtil.dip2px(36),0).setDuration(400).start();
                }
            }
        });

        mLayoutComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DynamicDetailVideoActivity.this, CommentActivity.class);
                startActivityForResult(intent, REQUEST_COMMENT);
            }
        });

        mLayoutLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                likeDynamic(mDynamicBean.getId());
            }
        });

        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        textViewContent.setText(mDynamicBean.getContent());
        videoView.setVideoURI(Uri.parse(mDynamicBean.getContentImgs().get(0).getUrl()));
        //videoView.setVideoURI(Uri.parse("http://www.w3school.com.cn/example/html5/mov_bbb.mp4"));
        videoView.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.stopPlayback();
    }

    @Override
    protected void onStop() {
        super.onStop();
        videoView.pause();
        isPause = true;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isPause) {
            videoView.start();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_COMMENT && resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");
            if (TextUtil.isNotEmpty(result)) {
                commentDynamic(mDynamicBean.getId(), 0, result);
            }
        }
    }

    private void commentDynamic(int dynamic,int userId, String content) {
        WPHSdk.getService(BusinessService.class).commentDynamic(dynamic, userId, content, new YRequestCallback<List<CommentBean>>() {
            @Override
            public void onSuccess(List<CommentBean> var1) {
                mDynamicBean.setReplyList(var1);
                RxBus.getInstance().send(SubscriptionBean.createSendBean(RxEventType.UPDATE_DYNAMIC,mDynamicBean));
            }

            @Override
            public void onFailed(int var1, String message) {
                ToastManager.getInstance().showToast(message);
            }

            @Override
            public void onException(Throwable var1) {
                var1.printStackTrace();
            }
        });
    }

    private void likeDynamic(int dynamicId) {
        WPHSdk.getService(BusinessService.class).addOrCancelLike(dynamicId, true, new YRequestCallback<List<ThumbUpBean>>() {
            @Override
            public void onSuccess(List<ThumbUpBean> var1) {
                mDynamicBean.setLikeList(var1);
                RxBus.getInstance().send(SubscriptionBean.createSendBean(RxEventType.UPDATE_DYNAMIC,mDynamicBean));
            }

            @Override
            public void onFailed(int var1, String message) {

            }

            @Override
            public void onException(Throwable var1) {
                var1.printStackTrace();
            }
        });
    }
}
